package com.swz.dcrm.ui.main;


import android.graphics.Typeface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


import com.swz.dcrm.R;
import com.swz.dcrm.model.Work;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.ui.home.TodayPlanFragmentArgs;
import com.swz.dcrm.ui.viewmodel.HomeViewModel;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.util.Constant;
import com.swz.dcrm.util.Tool;
import com.xh.baselibrary.widget.BounceScrollView;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment {


    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.toolbar)
    ConstraintLayout toolbar;
    @BindView(R.id.tv_work)
    TextView tvWork;
    @BindView(R.id.scroll)
    BounceScrollView scrollView;
    @BindView(R.id.tv_today_contact)
    TextView tvTodayContact;
    @BindView(R.id.tv_today_arrive)
    TextView tvTodayArrive;
    @BindView(R.id.tv_today_booking)
    TextView tvTodayBooking;
    @BindView(R.id.tv_nearly_delivery)
    TextView tvNearlyDelivery;
    @BindView(R.id.tv_add_target_one)
    TextView tvAddTarget1;
    @BindView(R.id.tv_add_target_two)
    TextView tvAddTarget2;
    @BindView(R.id.tv_add_target_three)
    TextView tvAddTarget3;

    @Inject
    MainViewModel mainViewModel;
    @Inject
    HomeViewModel homeViewModel;

    AbsoluteSizeSpan absoluteSizeSpan24;
    ForegroundColorSpan foregroundColorSpanlight;
    ForegroundColorSpan foregroundColorSpanBlue;
    StyleSpan styleSpanBold;


    public static HomeFragment getInstance(){
        return new HomeFragment();
    }


    @Override
    public void onResume() {
        onLoadRetry();
        super.onResume();
    }

    @Override
    public void onLoadRetry() {
        homeViewModel.getWorkCount().observe(this,workBaseResponse -> {
            if(workBaseResponse.isSuccess()){
                Work work=workBaseResponse.getData();
                String str=work.getContactCount().getHasFollowCount()+"/"+
                        work.getContactCount().getTotalFollowCount();
                SpannableString spannableString=new SpannableString(str);
                spannableString.setSpan(foregroundColorSpanlight, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(absoluteSizeSpan24, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(styleSpanBold, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvTodayContact.setText(spannableString);
                str=work.getComeShopCount().getHasFollowCount()+"/"+work.getComeShopCount().getTotalFollowCount();
                spannableString=new SpannableString(str);
                spannableString.setSpan(foregroundColorSpanlight, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(absoluteSizeSpan24, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(styleSpanBold, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvTodayArrive.setText(spannableString);
                str=work.getOrderCount().getHasFollowCount()+"/"+work.getOrderCount().getTotalFollowCount();
                spannableString=new SpannableString(str);
                spannableString.setSpan(foregroundColorSpanlight, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(absoluteSizeSpan24, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(styleSpanBold, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvTodayBooking.setText(spannableString);
                str=work.getTodaySendCarCount()+"/"+work.getSendCarCount7();
                spannableString=new SpannableString(str);
                spannableString.setSpan(foregroundColorSpanlight, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(absoluteSizeSpan24, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(styleSpanBold, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvNearlyDelivery.setText(spannableString);
                str=work.getMonthCreateFileCount().getAddCount()+"/"+work.getMonthCreateFileCount().getGoalCount();
                spannableString=new SpannableString(str);
                spannableString.setSpan(foregroundColorSpanBlue, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(absoluteSizeSpan24, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(styleSpanBold, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvAddTarget1.setText(spannableString);
                str=work.getMonthOrderCount().getAddCount()+"/"+work.getMonthOrderCount().getGoalCount();
                spannableString=new SpannableString(str);
                spannableString.setSpan(foregroundColorSpanBlue, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(absoluteSizeSpan24, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(styleSpanBold, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvAddTarget2.setText(spannableString);
                str=work.getMonthSendCarCount().getAddCount()+"/"+work.getMonthSendCarCount().getGoalCount();
                spannableString=new SpannableString(str);
                spannableString.setSpan(foregroundColorSpanBlue, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(absoluteSizeSpan24, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(styleSpanBold, 0,str.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvAddTarget3.setText(spannableString);

            }
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }
    @Override
    public int layoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
      //  title.setAlpha(0);
        styleSpanBold=new StyleSpan(Typeface.BOLD);
        absoluteSizeSpan24=new AbsoluteSizeSpan(Tool.dip2px(getActivity(),24));
        foregroundColorSpanlight=new ForegroundColorSpan(getResources().getColor(R.color.black_2f3234));
        foregroundColorSpanBlue=new ForegroundColorSpan(getResources().getColor(R.color.blue_0f6eff));
        toolbar.setAlpha(0);
        ConstraintLayout.LayoutParams layoutParams= (ConstraintLayout.LayoutParams) toolbar.getLayoutParams();
        layoutParams.height=getStatusBarHeight(getContext())+ Tool.dip2px(getActivity(),50);
        toolbar.setLayoutParams(layoutParams);
        toolbar.setPadding(0,getStatusBarHeight(getContext()),0,0);
        title.setText(getResources().getStringArray(R.array.main_tab_name)[0]);
        scrollView.setOnScrollChangeListener((BounceScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {


            if(scrollY<=tvWork.getBottom()){

                float y=scrollY;
                float bottom=tvWork.getBottom();
                Log.d("scrollView","scrollY="+scrollY+",bottom="+tvWork.getBottom());
                toolbar.setAlpha(y/bottom);
            }
        });
        return false;
    }
   @OnClick(R.id.c_today_contact)
    public void todayContact(){
       TodayPlanFragmentArgs.Builder builder=new TodayPlanFragmentArgs.Builder();
       builder.setType(Constant.TODAY_CONTACT);
       go(mainViewModel,R.id.action_mainFragment_to_todayContactFragment,builder.build().toBundle());
    }
    @OnClick(R.id.c_today_arrive)
    public void todayArrive(){

        TodayPlanFragmentArgs.Builder builder=new TodayPlanFragmentArgs.Builder();
        builder.setType(Constant.TODAY_ARRIVE);
        go(mainViewModel,R.id.action_mainFragment_to_todayContactFragment,builder.build().toBundle());
    }

    @OnClick(R.id.c_today_booking)
    public void todayBooking(){
        TodayPlanFragmentArgs.Builder builder=new TodayPlanFragmentArgs.Builder();
        builder.setType(Constant.TODAY_BOOKING);
        go(mainViewModel,R.id.action_mainFragment_to_todayContactFragment,builder.build().toBundle());
    }
    @OnClick(R.id.c_nearly_delivery)
    public void nearlyDelivery(){
        go(mainViewModel,R.id.action_mainFragment_to_nearlyDeliveryFragment,null);
    }
    @OnClick(R.id.tv_customer_manager)
    public void customerManager(){
        go(mainViewModel,R.id.action_mainFragment_to_customerManagementFragment,null);
    }
    @OnClick(R.id.tv_car_resource)
    public void carResource(){
        go(mainViewModel,R.id.action_mainFragment_to_carResFragment,null);
    }
    @OnClick(R.id.tv_electron_office)
    public void electronOffice(){
        go(mainViewModel,R.id.action_mainFragment_to_electronOfficeFragment,null);
    }

    @OnClick(R.id.tv_buy_car_cal)
    public void buyCarCal(){
        go(mainViewModel,R.id.action_mainFragment_to_buyCarCalFragment,null);
    }
}
